package com.example.oasystem.controller.login;

import com.example.oasystem.dao.login.AoaUserLoginRecordDao;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.user.AoaUserLoginRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LoginLoginRecordController {
    String sousuo=null;
    @Autowired
    AoaUserLoginRecordDao aoaUserLoginRecordDao;
    @RequestMapping("/morelogrecord")
    public String morelogrecord(HttpServletRequest request, Model model,@RequestParam(value = "baseKey",required = false,defaultValue = "") String baseKey){
        System.out.println("进来了");
        System.out.println("baseKey = " + baseKey);
        if (baseKey.equals(""))
        {
            baseKey=null;

        }
        else
        {
            sousuo=baseKey;
        }
        List<AoaUserLoginRecord> quan=aoaUserLoginRecordDao.selectQuan(baseKey);
        int totalElement= quan.size();
        int size=3;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=0;
        boolean first;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page page=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaUserLoginRecord> list=aoaUserLoginRecordDao.selectAll(baseKey,(number)*size,size);
        if(baseKey==null)
        {
            baseKey="";

        }
        model.addAttribute("userloglist",list);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",page);
        model.addAttribute("url","chazhaoForLogin");
        return "user/userlogrecordmanage";
    }
    @RequestMapping("/chazhaoForLogin")
    public String chazhao(int page,HttpServletRequest request, Model model){
        System.out.println("进来了");

        List<AoaUserLoginRecord> quan=aoaUserLoginRecordDao.selectQuan(sousuo);
        int totalElement= quan.size();
        int size=3;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=page;
        boolean first;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page fenye=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaUserLoginRecord> list=aoaUserLoginRecordDao.selectAll(sousuo,(number)*size,size);

        model.addAttribute("userloglist",list);
        model.addAttribute("baseKey",sousuo);
        model.addAttribute("page",fenye);
        model.addAttribute("url","chazhaoForLogin");
        return "user/userlogrecordmanagetable";
    }
}
